//
//  LingQuPerSonController.m
//  XiangCaoJiaYuan
//
//  Created by tongj on 2017/8/25.
//  Copyright © 2017年 tongj. All rights reserved.
//

#import "LingQuPerSonController.h"
#import "UIButton+LT.h"
#import "LingQuPerSonCell.h"
#import "LingquModel.h"
@interface LingQuPerSonController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource,MJRefreshBaseViewDelegate>
{
    NSMutableArray * datas;
    UICollectionView * _collectionView;
    UILabel *count1;
    UILabel *count2;
    MJRefreshHeaderView *_headerView;
    MJRefreshFooterView *_footerView;
    BOOL _isLoad;//是否处于网络请求状态
    NSInteger _pageIndex;
}
@property (nonatomic,strong)NSMutableArray *dataArray1;

@end

@implementation LingQuPerSonController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray1  = [[NSMutableArray alloc]init];
    _pageIndex = 0;
    _isLoad =NO;
    
    [self questData];

    self.title = @"领取用户";
    UIButton *backbutton = [[UIButton alloc]initWithFrame:CGRectMake(10, 20, 15, 25)];
    [backbutton setBackgroundImage:[UIImage imageNamed:@"香草园地-发布信息_03@2x"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(goBacktoUp) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backbutton];
    self.navigationItem.leftBarButtonItem = backItem;
    [self createCollectionView];
}
-(void)goBacktoUp{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)questData{

    NSString *url = [NSString stringWithFormat:@"%@redPacket/winners?redPacket_guid=%@&times=%ld",ServiceURL2,_guid,_pageIndex];
    [YBRequestManager GET:url finished:^(NSData *data) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [NSMutableArray array];
        NSArray *str11 = dict[@"data"];
        NSLog(@"dict==%@",dict);
        for (NSDictionary *dict1 in str11)
        {
            [array addObject:dict1];
        }
        if (_pageIndex==0) {
            [_dataArray1 removeAllObjects];
        }
        NSArray *array2 = [LingquModel arrayOfModelsFromDictionaries:array error:nil];
        [_dataArray1 addObjectsFromArray:array2];
        [_collectionView reloadData];
        _isLoad = NO;
        [_headerView endRefreshing];
        [_footerView endRefreshing];
        
    } failed:^(NSString *error) {
        
    }];
    
}
-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if (_isLoad) {
        return;
    }
    _isLoad = YES;
    //如果是下拉刷新
    
    
    if (refreshView==_headerView) {
        _pageIndex=0;
    }else if (refreshView==_footerView){
        _pageIndex++;
    }
    [self questData];
    
}
- (void)dealloc {
    [_headerView free];
    [_footerView free];
}
- (void)createCollectionView{
    
    
    UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen_WIDTH, 50*HEIGHT)];
    view.backgroundColor = kRGBColor(240,240,240);
    [self.view addSubview:view];
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
    count1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, UIScreen_WIDTH/2, 20)];
    count1.textAlignment = NSTextAlignmentCenter;
    count1.textColor = coloreg2;
    count1.text = [NSString stringWithFormat:@"投放个数：%d个",_packet_count];
    [view addSubview:count1];
    count2 = [[UILabel alloc]initWithFrame:CGRectMake(UIScreen_WIDTH/2, 15, UIScreen_WIDTH/2, 20)];
    count2.textAlignment = NSTextAlignmentCenter;
    count2.textColor = coloreg2;
    count2.text = [NSString stringWithFormat:@"已领取：%d个",_packet_count-_remain_num];
    [view addSubview:count2];

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, view.frame.size.height, UIScreen_WIDTH, UIScreen_HEIGHT-view.frame.size.height-64) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = colordfdfdf;
    _collectionView.alwaysBounceVertical = YES;

    //注册cell
    UINib * nib = [UINib nibWithNibName:@"LingQuPerSonCell" bundle:nil];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"LingQuPerSonCell"];
    
    [self.view addSubview:_collectionView];
    _headerView=[MJRefreshHeaderView header];
    _headerView.scrollView=_collectionView;
    _headerView.delegate=self;
    //上拉加载更多的视图
    _footerView=[MJRefreshFooterView footer];
    _footerView.scrollView=_collectionView;
    _footerView.delegate=self;
    
}
//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//    
//    return 10;
//}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _dataArray1.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LingQuPerSonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LingQuPerSonCell" forIndexPath:indexPath];
    LingquModel *model = _dataArray1[indexPath.row];
    [cell setModel:model];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(75*WIDTH,75*HEIGHT);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return CGSizeMake(self.view.bounds.size.width,0);
    }
    return CGSizeMake(self.view.bounds.size.width,10);
    
}
//-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(0,1,0,0);//分别为上、左、下、右
//    
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
